<?php

namespace App\Http\Controllers\Web\Buyer;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Repositories\Interfaces\ChangeRepository;
use App\Repository\Repositories\Interfaces\OrdersRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

/**
 * 退单管理控制器
 *
 * @package App\Http\Controllers\Backend
 */
class ChangeController extends BaseController
{
	/**
	 * @var ChangeRepository
	 */
	protected $change_repo;
	protected $user_id;
	protected $user_name;

	public function __construct(ChangeRepository $change_repo)
	{
		parent::__construct();

		$this->change_repo = $change_repo;
		$this->user_id = array_get($this->user, 'id');
		$this->user_name = array_get($this->user, 'name');

		//用户只能操作自身所属数据
		$this->change_repo->scopeQuery(function ($query) {
			return $query->where('user_id', $this->user_id);
		});
	}

	public function index(Request $request)
	{

		$change = $this->change_repo->with(['orderProducts','order'])->paginate($this->perPage);
//		return $this->success(ErrorConst::SUCCESS_CODE_MSG, $change);
		return $this->success(ErrorConst::SUCCESS_CODE, $change,true);

	}

	public function show($id)
	{
		$change = $this->change_repo->with(['orderProducts'])->scopeQuery(function ($query) {
			return $query->where('user_id', $this->user_id);
		})->find($id);
		return $this->success(ErrorConst::SUCCESS_CODE_MSG, $change['data']);
	}

	/**
	 * 创建退单，现阶段只允许整单退款
	 * @param integer $shop_id 店铺id
	 * @param  string $order_code 订单code
	 * @param  string $order_id 订单id
	 * @param  string $change_money 退款金额
	 * @param  string $resason 退款原因
	 * @param  string $images 退款图片
	 * @param  string $explain 退款说明
	 * @return susses
	 */

	public function store(OrdersRepository $order_rpo, Request $request)
	{

		$change_data = [
//			"user_id" => $request->session()->get('userid'),
//			"user_name" => $request->session()->get('username'),
			"user_id" => $this->user_id,
			"user_name" => $this->user_name,
			"shop_id" => $request->post('shop_id'),
			"change_code" => newCode(),
			"change_type" => $request->post('change_type'),
			"order_code" => $request->post('order_code'),
			"order_id" => $request->post('order_id'),
			"change_money" => $request->post('change_money'),
			"change_product" => $request->post('change_product'),
			"num" => $request->post('num'),
			"resason" => $request->post('resason'),
			'images' => $request->post('images'),
			"explain" => $request->post('explain'),
			"status" => 0,
		];
//		$change_data = $request->all();
//		dd($change_data);
		$order = $order_rpo->with(['orderProducts' => function ($sql) {
			$sql->select(['product_name', 'quantity', 'product_money', 'shop_name', 'order_code']);
		}])->find($change_data['order_id'])['data'];

//		dd($order);
		//判断用户，商家，订单，金额是否匹配，不匹配返回错误
		if ($change_data['user_id'] == $order['user_id'] && $change_data['shop_id'] == $order['shop_id'] && $change_data['order_code'] == $order['order_code'] && $change_data['change_money'] == $order['product_money']) {
			$ret = true;
		} else {
			return $this->noDataReturn(false);
		}
		//取店铺和商品
		$change_data['shop_name'] = $order['order_products'][0]['shop_name'];
		$change_data['change_product'] = $order['order_products'];
		$change_data['change_product'] = is_array($change_data['change_product']) ? json_encode($change_data['change_product'], true) : $change_data['change_product'];
		//判断创建退单状态是否合法，未发货为0退款，已发货为2退货，暂时没有1部分退货
		if($order['order_status'] == 2 && $change_data['change_type'] == 0 ){
			$ret=true;
		}elseif($order['order_status'] == 3 && $change_data['change_type'] == 2){
			$ret=true;
		}else{
			dd(1111);

			return $this->noDataReturn(false);
		}
		$change_data['images'] = is_array($change_data['images']) ? json_encode($change_data['images'], true) : $change_data['images'];
		\DB::beginTransaction();
		//创建退单
		$ret = $this->change_repo->create($change_data);
		//修改订单状态为申请退货退款
		$ret = $order_rpo->update(['change_status'=> 1],$order['id']);
		if($ret){
			\DB::commit();//事务提交；
			return $this->noDataReturn(true);
		}else{
			\DB::rollback(); //事务回滚;
			return $this->noDataReturn(false);
		}
	}

	public function update(Request $request)
	{
		//退款退货订单生成后，用户现在只有
		$this->validate($request, [
			'status' => 'required',
			'change_id' => 'required',
			'user_id' => 'required'
		]);
		$data = $request->all();
		$change_order = $this->change_repo->find($data['change_id']);
		if ($data['user_id'] == $change_order['data']['user_id'] && $change_order['data']['status'] == 0 && $data['status'] == 4) {
			$ret = true;
		} else {
			return $this->noDataReturn(false);
		}
		
		$updata=[
		'status'=>$data['status']
		];
		$ret = $this->change_repo->update($updata, $data['change_id']);

		return $this->noDataReturn($ret ? true : false);
	}

	public function delete($id)
	{
		//用户无删除订单操作，以下作为样例
//		$ret = $this->change_repo->delete($id);
//
		return $this->noDataReturn(false);
	}

}
